//Demo
layui.use('form', function(){
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function(data){
        layer.msg(JSON.stringify(data.field));
        return false;
    });
});

//表单
layui.use('table', function(){
    var table = layui.table;
});

//添加栏目
$(function () {
    $("#add").click(function () {
        var add_tags = layer.open({
            type:1,
            area:['350px','350px'],
            title: "新增标签"
            ,content: $("#add_category"),
            shade: 0,
            btn: ['提交', '重置']
            ,btn1: function(index, layero){//提交按钮
                var name = $("#name").val()
                var description = $("#description").val()
                var alias = $("#alias").val()
                var sort = $("#sort").val()

                if(name == null || name == ""){
                    alert("栏目名称不能为空")
                    return false;
                }
                if(description == null || description == ""){
                    alert("栏目描述不能为空")
                    return false;
                }
                if(alias == null || alias == ""){
                    alert("栏目别名不能为空")
                    return false;
                }
                if(sort == null || sort == ""){
                    alert("栏目排序依据")
                    return false;
                }

                var formData = {
                    name: name,
                    description: description,
                    alias: alias,
                    sort: sort
                }
                $.ajax({
                    url: '/admin/category/add',
                    method: "post",
                    data: formData,
                    success: (response) => {
                        if (response.code == 0) {
                            alert("创建栏目成功")
                            location.reload()
                        } else {
                            alert(response.msg)
                            return false
                        }
                    },
                })
            },
            btn2: function(index, layero){//重置按钮
                $("#name").val("")
                $("#description").val("")
            },
            cancel: function(layero,index){//关闭按钮
                layer.close(add_tags);
            }
        });
    })

})

